{% extends 'iframe/basePlusPlus.html.twig' %}

{% block cardHeader1 %}模型{% endblock %}

{% block cardBody1 %}
<table class="layui-table">
    <colgroup>
        <col width="200">
        <col width="2000">
    </colgroup>
    <tr>
        <td>ID</td>
        <td>{{ model.id }}</td>
    </tr>
    <tr>
        <td>名称</td>
        <td>{{ model.dry_name }}</td>
    </tr>
    <tr>
        <td>数据库配置键</td>
        <td>{{ model.dry_database_config_key }}</td>
    </tr>
    <tr>
        <td>数据库</td>
        <td>{{ model.dry_database }}</td>
    </tr>
    <tr>
        <td>表名称</td>
        <td>{{ model.dry_table }}</td>
    </tr>
    <tr>
        <td>别名</td>
        <td>{{ model.dry_alias }}</td>
    </tr>
    <tr>
        <td>控制器名称</td>
        <td>{{ model.dry_controller_name }}</td>
    </tr>
    <tr>
        <td>备注</td>
        <td>{{ model.dry_note }}</td>
    </tr>
    <tr>
        <td>排序</td>
        <td>{{ model.dry_sort }}</td>
    </tr>
    <tr>
        <td>添加时间</td>
        <td>{{ model.dry_add_time }}</td>
    </tr>
    <tr>
        <td>表是否存在</td>
        <td>{{ table_exist_text }}</td>
    </tr>
    {% if not table_exist %}
        <tr>
            <td>SQL</td>
            <td>{{ create_table_sql }}</td>
        </tr>
        <tr>
            <td>操作</td>
            <td><button type="button" class="layui-btn" onclick="createTable(this, {{ model.id }})">执行</button></td>
        </tr>
    {% endif %}
</table>
{% endblock %}

{% block cardHeader2 %}{% if table_exist %}字段{% endif %}{% endblock %}

{% block cardBody2 %}
{% if table_exist %}
<!---->
<table class="layui-table">
    <thead>
    <tr>
        <th>序号</th>
        <th>字段</th>
        <th>当前类型</th>
        <th>目标类型</th>
        <th>是否存在</th>
        <th>是否需要</th>
        <th>SQL</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {% for rs in list %}
        <tr>
            <td>{{ rs.dry_no }}</td>
            <td>{{ rs.dry_field }}</td>
            <td>{{ rs.dry_current_type }}</td>
            <td>{{ rs.dry_target_type }}</td>
            <td>{{ rs.exist_text }}</td>
            <td>{{ rs.require_text }}</td>
            <td>{{ rs.sql }}</td>
            {% if rs.sql == '' %}
                <td><button type="button" class="layui-btn layui-btn-disabled">执行</button></td>
            {% else %}
                <td><button type="button" class="layui-btn" onclick="adjustField(this, {{ model.id }}, '{{ rs.dry_field }}')">执行</button></td>
            {% endif %}
        </tr>
    {% endfor %}
    </tbody>
</table>
<!---->
{% endif %}
{% endblock %}

{% block footer %}
<script>
var layer = null;

layui.use(['layer'], function(){
    layer = layui.layer;
});

function reload()
{
    setTimeout(function(){
        window.location.reload();
    }, 2000);
}

function success(response)
{
    if(response.ok == true && response.status == 200){
        response.json().then(function(data){
            if(data.code != 0){
                layer.msg(data.msg);
            }
            else{
                layer.msg("操作成功");
            }
            reload();
        });
    }
}

function fail(response)
{
    layer.msg("操作失败，请重试");
    reload();
}

function createTable(that, modelId)
{
    that.style.display = 'none';
    fetch("{{ get_domain() }}/Admin/Model/createTable?modelId=" + modelId).then(success).catch(fail);
}

function adjustField(that, modelId, field)
{
    that.style.display = 'none';
    fetch("{{ get_domain() }}/Admin/Model/adjustField?modelId=" + modelId + "&field=" + field).then(success).catch(fail);
}
</script>
{% endblock %}